package com.datastructure.test.cutwood;

import java.util.ArrayList;
import java.util.Collections;

public class CutWood {

    public static void main(String[] args) {
        CutWood cutWood = new CutWood();
        ArrayList<Integer> a = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            a.add(i);
        }
        int wood = cutWood.cutWood(a, 7);
        System.out.println(wood);
    }

    public int cutWood (ArrayList<Integer> a, int k) {
        // write code here
        Collections.sort(a);
        int left = a.get(0);
        int right = a.get(a.size()-1);
        while (left<right) {
            int mid = (left + right+1)/2;
            int sum = 0;
            for (int x: a) {
                sum += x/mid;
            }
            if (sum >= k) {
                left = mid;
            } else {
                right = mid-1;
            }
        }
        return left;
    }
}
